package grpc

import (
	"context"
	"gitee.com/kmyss/gf-ex/yscmd"
	"gitee.com/kmyss/gf-ex/yslog"
	"gitee.com/kmyss/ys-cli/internal/model"
	"gitee.com/kmyss/ys-cli/internal/service"
)

type Builder struct {
	yscmd.CommandBuilderCore
}

func (b *Builder) Init(ctx context.Context) {
	b.CommandBuilderCore.Init(ctx)
	b.C.Use = "grpc"
	b.C.Short = "启动 grpc 服务"
	b.C.Long = `
    - 可以启动 mq 服务`

	b.C.Run = run
	b.addFlag()
}

func (b *Builder) addFlag() {
	b.SetFlags(&FlagMqBuilder{})
	b.SetFlag(&yscmd.FlagDebugBuilder{})
}

func run(c *yscmd.Command) {
	err := service.Grpc().Run(c.Context(), &model.GRPCCfgInput{RunMQ: FlagMq(c.Context()).Has()})
	if err != nil {
		yslog.Error("GRPC 服务启动失败！", err)
		yscmd.CmdWait(c.Context())
		return
	}

	yscmd.TimeToCloseShell(c.Context(), 2)
}

func Grpc(ctx context.Context) *yscmd.Command {
	c := &Builder{}
	c.Init(ctx)
	return c.Command()
}
